package biz.yfsoft.app.fastframework.admin;


import java.text.MessageFormat;
import java.util.Enumeration;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import biz.yfsoft.app.fastframework.bo.Bo;
import biz.yfsoft.app.fastframework.interceptor.AdminSessionInterceptor;

import com.alibaba.fastjson.JSONObject;
import com.jfinal.aop.Before;
import com.jfinal.core.Controller;
import com.jfinal.kit.StrKit;
import com.jfinal.plugin.activerecord.ActiveRecordException;
import com.jfinal.plugin.activerecord.Db;
import com.jfinal.plugin.activerecord.Page;
import com.jfinal.plugin.activerecord.Record;

/**
 * 用于后台管理的控制器的抽象
 * @author zt
 *
 */
@Before(AdminSessionInterceptor.class)
public abstract class AdminController extends Controller {
	
	protected String tableName(){
		return "";
	}
	
	protected Map<String,Object> prevAdd(Map<String,Object> args) {
		return args;
	}
	//将配置信息加入到配置中
	public void c(String key,Object val){
		
	}
	
	/**
	 * 从配置集中获取配置信息
	 * @param key
	 * @return
	 */
	public Object c(String key){
		Record r = Db.findFirst("select * from sys_setting where name = ? and status > 0", key);
		return r.get("val");
	}
	
	public String i(String key){
		return getPara(key);
	}

	public Map<String, Object> getParams() {
		Map<String, Object> map = new HashMap<String,Object>();
		for(Enumeration<String> names = super.getParaNames();names.hasMoreElements();){
			String name = names.nextElement();
			map.put(name, getPara(name));
		}
		return map;
	}
	
	public String wrapJson(int code,String msg,Object data,List<?> rows){
		JSONObject jo = new JSONObject();
		jo.put("code", code);
		if(StrKit.notBlank(msg))
			jo.put("msg",msg);
		if(null!=data)
			jo.put("data",data);
		if(null!=rows)
			jo.put("rows",rows);
		return jo.toJSONString();
	}
	
	public String wrapJson(int code,String msg,Object data){
		return wrapJson(code,msg,data,null);
	}
	
	public String wrapJson(int code,String msg){
		return wrapJson(code,msg,null,null);
	}
	
	public String wrapJson(int code){
		return wrapJson(code,"",null,null);
	}
	
	public void add(){
		Map<String,Object> args = prevAdd(getParams());
		Bo bo = new Bo().create(args);
		try{
			boolean flag = Db.save(tableName(), bo);
			if(flag){
				long id = bo.get(Bo.ID);
				renderJson(wrapJson(0,"",id));
			}else{
				renderJson(wrapJson(-1,"操作失败!"));
			}
		}catch(ActiveRecordException hex){
			renderJson(wrapJson(-2,"操作失败!可能原因如下：\n"+hex.getMessage()));
		}
	}
	
	public void ajaxList() {
		int limit = getParaToInt("limit",10);//每页显示的条数
		int offset = getParaToInt("offset",0);//从0开始偏移
		String sort = getPara("sort");//获取排序的列
		String order = getPara("order");//升降序
		String orderby = "";//排序的sql
		if(StrKit.notNull(sort)){
			//需要进行排序
			orderby = MessageFormat.format("order by {0} {1}",sort,order);
		}
		int page = offset/limit+1;
		Page<Record> pageRecord = Db.paginate(page, limit, "select *", "from "+tableName()+" where status>0 "+orderby);
		JSONObject rst = new JSONObject();
		rst.put("total", pageRecord.getTotalRow());
		rst.put("rows", pageRecord.getList());
		renderJson(rst);
	}
	
	public void del(){
		String ids = getPara("id[]");
		int rows = Db.update("update "+tableName()+" set status= -1 where id in ("+ ids+")");
		if(rows>0){
			renderJson(wrapJson(0,"",rows));
		}else{
			renderJson(wrapJson(-1,"删除失败!"));
		}
	}
	
	public void edit(){
		String id = getPara("id");
		Map<String,Object> args = getParams();
		args.put(Bo.UPDATE_TIME, System.currentTimeMillis());
		Record record = Db.findById(tableName(), id);
		record.setColumns(args);
		if(Db.update(tableName(), record)){
			renderJson(wrapJson(0,"",record));
		}else{
			renderJson(wrapJson(-1,"修改失败!"));
		}
	}
	
}

